package com.hakim.wfb.admin.service.impl;

import com.hakim.wfb.admin.params.interest.*;
import com.hakim.wfb.admin.result.interest.ConsumeInterestView;
import com.hakim.wfb.admin.service.api.ConsumeInterestService;
import com.hakim.wfb.bean.ConsumeInterest;
import com.hakim.wfb.customer.enums.ErrorCodeEnum;
import com.hakim.wfb.dao.ConsumeInterestDao;
import com.hkfs.fundamental.api.data.ListResultEx;
import com.hkfs.fundamental.api.data.ObjectResultEx;
import com.hkfs.fundamental.api.data.ResultEx;
import com.hkfs.fundamental.api.params.SessionIdentity;
import com.hkfs.fundamental.common.utils.TimeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by walkman on 2017/7/27.
 */
@Service("adminInterestService")
public class ConsumeInterestServiceImpl implements ConsumeInterestService {

    @Autowired
    private ConsumeInterestDao consumeInterestDao;

    /**
     * @param param
     * @return
     */
    @Override
    public ResultEx addInterest(AddConsumeInterestParam param) {

        ResultEx resultEx = new ResultEx();
        GetConsumeInterestParam getConsumeInterestParam = new GetConsumeInterestParam();
        getConsumeInterestParam.setRuleId(param.getRuleId());
        if (!StringUtils.isBlank(param.getInterestDate())) {
            getConsumeInterestParam.setInterestDateTime(TimeUtils.parseDate(param.getInterestDate(), TimeUtils.FORMAT_YYYYMMDD));
        }
        if (getConsumeInterestParam.getInterestDateTime().before(TimeUtils.getDaysDate(new Date()))) {
            resultEx.setRetCode(ErrorCodeEnum.CONSUME_INTEREST_ADD_EXPIRED_ERROR.getCode());
            resultEx.setRetMsg(ErrorCodeEnum.CONSUME_INTEREST_ADD_EXPIRED_ERROR.getMessage());
            return resultEx.makeFailedResult();
        }
        ConsumeInterest consumeInterestUpdate = consumeInterestDao.getConsumeInterest(getConsumeInterestParam);
        if (consumeInterestUpdate != null) {
            resultEx.setRetCode(ErrorCodeEnum.CONSUME_INTEREST_EXIST_ERROR.getCode());
            resultEx.setRetMsg(ErrorCodeEnum.CONSUME_INTEREST_EXIST_ERROR.getMessage());
            return resultEx.makeFailedResult();
        }
        ConsumeInterest consumeInterest = new ConsumeInterest();
        BeanUtils.copyProperties(param, consumeInterest);
        if (!StringUtils.isBlank(param.getInterestDate())) {
            consumeInterest.setInterestDate(TimeUtils.parseDate(param.getInterestDate(), TimeUtils.FORMAT_YYYYMMDD));
        }
        SessionIdentity sessionIdentity = param.getSessionIdentity();
        Date now = new Date();
        consumeInterest.setCreatedBy(sessionIdentity.getUserId());
        consumeInterest.setCreatedTime(now);
        consumeInterest.setUpdatedBy(sessionIdentity.getUserId());
        consumeInterest.setUpdatedTime(now);
        consumeInterestDao.add(consumeInterest);
        return resultEx.makeSuccessResult();
    }

    /**
     * @param param
     * @return
     */
    @Override
    public ResultEx updateInterest(UpdateConsumeInterestParam param) {
        ResultEx resultEx = new ResultEx();

        ConsumeInterest consumeInterest = consumeInterestDao.getById(param.getInterestId());
        if (consumeInterest == null) {
            resultEx.setRetCode(ErrorCodeEnum.CONSUME_INTEREST_NOT_EXIST_ERROR.getCode());
            resultEx.setRetMsg(ErrorCodeEnum.CONSUME_INTEREST_NOT_EXIST_ERROR.getMessage());
            return resultEx.makeFailedResult();
        }
        ConsumeInterest consumeInterestUpdate = new ConsumeInterest();
        consumeInterestUpdate.setTid(param.getInterestId());
        consumeInterestUpdate.setRuleId(param.getRuleId());
        if (!StringUtils.isBlank(param.getInterestDate())) {
            consumeInterestUpdate.setInterestDate(TimeUtils.parseDate(param.getInterestDate(), TimeUtils.FORMAT_YYYYMMDD));
        }
        if (consumeInterest.getInterestDate().before(TimeUtils.getDaysDate(new Date()))) {
            resultEx.setRetCode(ErrorCodeEnum.CONSUME_INTEREST_UPDATE_EXPIRED_ERROR.getCode());
            resultEx.setRetMsg(ErrorCodeEnum.CONSUME_INTEREST_UPDATE_EXPIRED_ERROR.getMessage());
            return resultEx.makeFailedResult();
        }
        consumeInterestUpdate.setInterest(param.getInterest());
        SessionIdentity sessionIdentity = param.getSessionIdentity();
        Date now = new Date();
        consumeInterestUpdate.setUpdatedBy(sessionIdentity.getUserId());
        consumeInterestUpdate.setUpdatedTime(now);
        consumeInterestDao.update(consumeInterestUpdate);
        return resultEx.makeSuccessResult();
    }

    /**
     * @param param
     * @return
     */
    @Override
    public ResultEx deleteInterest(DeleteConsumeInterestParam param) {

        ResultEx resultEx = new ResultEx();

        ConsumeInterest consumeInterest = consumeInterestDao.getById(param.getInterestId());
        if (consumeInterest == null) {
            resultEx.setRetCode(ErrorCodeEnum.CONSUME_INTEREST_NOT_EXIST_ERROR.getCode());
            resultEx.setRetMsg(ErrorCodeEnum.CONSUME_INTEREST_NOT_EXIST_ERROR.getMessage());
            return resultEx.makeFailedResult();
        }
        if (consumeInterest.getInterestDate().before(TimeUtils.getDaysDate(new Date()))) {
            resultEx.setRetCode(ErrorCodeEnum.CONSUME_INTEREST_DELETE_EXPIRED_ERROR.getCode());
            resultEx.setRetMsg(ErrorCodeEnum.CONSUME_INTEREST_DELETE_EXPIRED_ERROR.getMessage());
            return resultEx.makeFailedResult();
        }
        int count = consumeInterestDao.delete(param.getInterestId());
        if (count <= 0) {
            resultEx.setRetMsg("删除失败");
            return resultEx.makeFailedResult();
        }
        return resultEx.makeSuccessResult();
    }

    /**
     * @param param
     * @return
     */
    @Override
    public ObjectResultEx<ConsumeInterestView> getInterest(GetConsumeInterestParam param) {
        ObjectResultEx<ConsumeInterestView> resultEx = new ObjectResultEx<>();

        if (!StringUtils.isBlank(param.getInterestDate())) {
            param.setInterestDateTime(TimeUtils.parseDate(param.getInterestDate(), TimeUtils.FORMAT_YYYYMMDD));
        }
        ConsumeInterest consumeInterest = consumeInterestDao.getConsumeInterest(param);
        if (consumeInterest != null) {
            ConsumeInterestView consumeInterestView = new ConsumeInterestView();
            consumeInterestView.setInterestId(consumeInterest.getTid());
            consumeInterestView.setRuleId(consumeInterest.getRuleId());
            consumeInterestView.setInterestDate(consumeInterest.getInterestDate());
            consumeInterestView.setInterest(consumeInterest.getInterest());
            resultEx.setDataObject(consumeInterestView);
            return resultEx.makeSuccessResult();
        }
        return resultEx.makeFailedResult();
    }

    /**
     * @param param
     * @return
     */
    public ListResultEx<ConsumeInterestView> queryInterest(QueryConsumeInterestParamWebPage param) {

        ListResultEx<ConsumeInterestView> resultEx = new ListResultEx<>();
        if (!StringUtils.isBlank(param.getInterestDateStart())) {
            param.setInterestDateTimeStart(TimeUtils.parseDate(param.getInterestDateStart(), TimeUtils.FORMAT_YYYYMMDD));
        }
        if (!StringUtils.isBlank(param.getInterestDateEnd())) {
            param.setInterestDateTimeEnd(TimeUtils.parseDate(param.getInterestDateEnd(), TimeUtils.FORMAT_YYYYMMDD));
        }
        List<ConsumeInterestView> consumeInterestViewList = consumeInterestDao.queryConsumeInterest(param);
        resultEx.setDataList(consumeInterestViewList);
        resultEx.setTotalCount(consumeInterestDao.countQuery(param));
        return resultEx.makeSuccessResult();
    }
}
